#include "HiKernel/HiFramework.h"
#include "HiKernel/HiGBRenderer.h"
#include "HiModules/mHiBikeController/mHiBikeController.h"
#include "HiModules/mHiDummy/LM_Anim.h"

#include <NiApplication.h>



mHiBikeController::mHiBikeController(HiXmlNode*xml)
{
	m_vVec3f = new HiSFVec3f(this, 1, "bikexyz");
	m_vTrans = D3DXVECTOR3(0.0f, 0.0f, 0.0f);

	m_fUnitMoveX=0.0f;
	m_fUnitMoveY=0.0f;
	m_fUnitMoveZ=0.0f;

	NiInit();

	if(xml!=NULL)
		ParseXml(xml);
}

bool mHiBikeController::HandleMessage(const Telegram& msg)
{

	HiGBRenderer* gbRender = (HiGBRenderer*)HiViewer::GetInstance()->GetRenderer();

	if ( gbRender->GetRenderType() != HiRenderer::Gamebryo )
		exit(-1);

	HiModule *pSender = ModuleManager->FindModule(msg.Sender);

	std::cout << pSender->GetName().c_str() << " -- - " <<  msg.Msg << std::endl;

	switch ( msg.Msg )
	{
		
	}

	return true;
}
int mHiBikeController::ParseXml(HiXmlNode*xml)
{

	return 1;
}



void
mHiBikeController::PostConfig()
{


	HiGBRenderer* gbRender = (HiGBRenderer*)HiViewer::GetInstance()->GetRenderer();

	if ( gbRender->GetRenderType() != HiRenderer::Gamebryo )
		exit(-1);


}

void mHiBikeController::Update()
{
	HiGBRenderer* gbRender = (HiGBRenderer*)HiViewer::GetInstance()->GetRenderer();

	if ( gbRender->GetRenderType() != HiRenderer::Gamebryo )
		exit(-1);


	NiInputGamePad* pkGamePad = NULL; 
	NiInputKeyboard* pkKeyboard = NULL;

	if(gbRender->m_spInputSystem )
	{
		if (gbRender->m_spInputSystem->GetMode() == NiInputSystem::MAPPED)
		{
			if (gbRender->m_spInputSystem->UpdateActionMap() == NIIERR_DEVICELOST)
				gbRender->m_spInputSystem->HandleDeviceChanges();
		}
		else
		{
			if (gbRender->m_spInputSystem->UpdateAllDevices() == NIIERR_DEVICELOST)
				gbRender->m_spInputSystem->HandleDeviceChanges();
		}
	}
	
	if (gbRender->GetInputSystem())
	{
		pkKeyboard = gbRender->GetInputSystem()->GetKeyboard();
	}
	if (pkKeyboard != NULL)
	{
		if (pkKeyboard->KeyIsDown(NiInputKeyboard::KEY_UP))
		{
			m_fUnitMoveX = m_fUnitMoveX + 1.0f;

		}
		if (pkKeyboard->KeyIsDown(NiInputKeyboard::KEY_DOWN))
		{
			m_fUnitMoveX = m_fUnitMoveX - 1.0f;
		}

		if (pkKeyboard->KeyIsDown(NiInputKeyboard::KEY_LEFT))
		{

			m_fUnitMoveY = m_fUnitMoveY + 1.0f;
		}
		if (pkKeyboard->KeyIsDown(NiInputKeyboard::KEY_RIGHT))
		{

			m_fUnitMoveY = m_fUnitMoveY - 1.0f;
		}
	}

	m_vTrans.x=m_fUnitMoveX;
	m_vTrans.y=m_fUnitMoveY;
	m_vTrans.z=m_fUnitMoveZ;

	m_vVec3f->Set(m_vTrans);

	Realize();

}
void mHiBikeController::Realize()
{
	this->SendEvent(m_vVec3f);
}

void mHiBikeController::Render()
{

}

extern "C" HI_DLLEXPORT
mHiBikeController* HiLoadModule_mHiBikeController(HiXmlNode* xml)
{
	mHiBikeController *m= new mHiBikeController(xml);
	return m;
}




